package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.LatentClient;
import cn.wolfcode.crm.query.LatentClientQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.Dispose;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("stateClient")
public class StateController {
    @Autowired
    private ILatentClientService latentClientService;

    @RequestMapping("outView")
    @RequiresPermissions(value = {"latentClient:outView", "潜在客户按钮"}, logical = Logical.OR)
    public String stateView() {
        return "stateClient/list";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"latentClient:query", "潜在客户查询"}, logical = Logical.OR)
    public PageResult query(LatentClientQueryObject qo) {
        return latentClientService.query(qo);
    }

    @RequestMapping("changStatus")
    @ResponseBody
    @RequiresPermissions(value = {"latentClient:changStatus", "把客户转如资源池"}, logical = Logical.OR)
    public JsonResult changStatus(Long id) {
        JsonResult result = new JsonResult();
        try {
            latentClientService.changStatus(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    //@RequiresPermissions(value = {"latentClient:view", "潜在用户的更新与修改"}, logical = Logical.OR)
    @RequiresPermissions("latentClient:view")
    public Dispose saveOrUpdate(LatentClient entity) {
        Dispose result = new Dispose();
        try {
            if (entity.getId() == null) {
                latentClientService.insert(entity);
            }else {
                latentClientService.updateByPrimaryKey(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("保存失败");
        }
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<LatentClient> listAll() {
        return latentClientService.listAll();
    }
    @RequestMapping("plan")
    @ResponseBody
    @RequiresPermissions(value = {"latentClient:plan", "把客户列入开发计划"}, logical = Logical.OR)
    public JsonResult  plan(Long id) {
        JsonResult result = new JsonResult();
        try {
            latentClientService.plan(id);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }

}
